﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Settings;

namespace NetfractionHub.Network.Protocol.PING.Extension
{
	public enum PINGHubINFParameterEnum : int
	{
		HubHostAddress = 1,
		HubWebsite = 2,
		HubNetwork = 3,
		HubOwnerName = 4,
		CurrentUserCount = 5,
		TotalShareSize = 6,
		TotalFilesShared = 7,
		MinimumShareRequired = 8,
		MaximumShareAllowed = 9,
		MinimumSlotsRequired = 10,
		MaximumSlotsAllowed = 11,
		MinimumHubsWhereUser = 12,
		MinimumHubsWhereRegistered = 13,
		MinimumHubsWhereOperator = 14,
		MaximumHubsWhereUser = 15,
		MaximumHubsWhereRegistered = 16,
		MaximumHubsWhereOperator = 17,
		MaximimumUserCount = 18,
		HubUptime = 19
	}

	// This is an INF extension, but is called in the SUP handler since it is the hub's INF. Thus, all these references are to the SUP message.
	[NetfractionHub.Network.Protocol.MessageExtension(MessageName = "SUP", ExtensionType = (int)NetfractionHub.Network.Protocol.BASE.Extensibility.HubINFExtensionType.Parameter)]
	public class ExtensionINFParameter : ProtocolMessageActionHandler
	{

		public override string ProtocolActionCode
		{
			get { return "SUP"; }
		}

		public override object HandleMessageExtension(RemoteMachine.Node node, ProtocolMessage message, params object[] args)
		{
			List<ProtocolParameter> extraParams = new List<ProtocolParameter>();
						
			System.Net.EndPoint ep = HubSettings.HubConnection.ListeningEndPoints.First();
			string hostAddress = string.Empty;
			if (ep is System.Net.DnsEndPoint)
				hostAddress = string.Format("{0}:{1}", (ep as System.Net.IPEndPoint).Address, (ep as System.Net.IPEndPoint).Port);
			else if (ep is System.Net.IPEndPoint)
				hostAddress = string.Format("{0}:{1}", (ep as System.Net.DnsEndPoint).Host, (ep as System.Net.DnsEndPoint).Port);

			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.HubHostAddress), hostAddress));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.HubWebsite), HubSettings.HubIdentity.Website));
			//extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.HubNetwork), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.HubOwnerName), HubSettings.HubIdentity.HubOwner));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.CurrentUserCount), ConnectionManager.ConnectedNodeCount.ToString()));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.TotalShareSize), ConnectionManager.TotalShareSize.ToString()));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.TotalFilesShared), ConnectionManager.TotalFilesShared.ToString()));

			if (HubSettings.SharingRules.MinimumShareSize > 0)
				extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MinimumShareRequired), HubSettings.SharingRules.MinimumShareSize.ToString()));

			/*extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MaximumShareAllowed), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MinimumSlotsRequired), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MaximumSlotsAllowed), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MinimumHubsWhereUser), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MinimumHubsWhereRegistered), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MinimumHubsWhereOperator), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MaximumHubsWhereUser), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MaximumHubsWhereRegistered), string.Empty));
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.MaximumHubsWhereOperator), string.Empty));*/
			extraParams.Add(new ProtocolParameter(GetParameterCodeForParameter(PINGHubINFParameterEnum.HubUptime), DateTime.Now.Subtract(Network.ConnectionManager.Hub.StartTime).ToString()));
			
			return extraParams;
		}

		private static string GetParameterCodeForParameter(PINGHubINFParameterEnum param)
		{
			switch (param)
			{
				case PINGHubINFParameterEnum.HubHostAddress:
					return "HH";
				case PINGHubINFParameterEnum.HubWebsite:
					return "WS";
				case PINGHubINFParameterEnum.HubNetwork:
					return "NE";
				case PINGHubINFParameterEnum.HubOwnerName:
					return "OW";
				case PINGHubINFParameterEnum.CurrentUserCount:
					return "UC";
				case PINGHubINFParameterEnum.TotalShareSize:
					return "SS";
				case PINGHubINFParameterEnum.TotalFilesShared:
					return "SF";
				case PINGHubINFParameterEnum.MinimumShareRequired:
					return "MS";
				case PINGHubINFParameterEnum.MaximumShareAllowed:
					return "XS";
				case PINGHubINFParameterEnum.MinimumSlotsRequired:
					return "ML";
				case PINGHubINFParameterEnum.MaximumSlotsAllowed:
					return "XL";
				case PINGHubINFParameterEnum.MinimumHubsWhereUser:
					return "MU";
				case PINGHubINFParameterEnum.MinimumHubsWhereRegistered:
					return "MR";
				case PINGHubINFParameterEnum.MinimumHubsWhereOperator:
					return "MO";
				case PINGHubINFParameterEnum.MaximumHubsWhereUser:
					return "XU";
				case PINGHubINFParameterEnum.MaximumHubsWhereRegistered:
					return "XR";
				case PINGHubINFParameterEnum.MaximumHubsWhereOperator:
					return "XO";
				case PINGHubINFParameterEnum.MaximimumUserCount:
					return "MC";
				case PINGHubINFParameterEnum.HubUptime:
					return "UP";
				default:
					return string.Empty;
			}
		}
	}
}
